package lc.q150_200;

public class Q213 {

    public static void main(String[] args) {
        System.out.println(new Q213().rob(new int[]{1,2,3,1,5}));
    }

    public int rob(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        if (nums.length == 1) {
            return nums[0];
        }
        if (nums.length == 2) {
            return Integer.max(nums[0], nums[1]);
        }
        return Integer.max(robPart(nums, 0, nums.length - 2), robPart(nums, 1, nums.length - 1));
    }

    private int robPart(int[] nums, int start, int end) {
        int ppv = 0;
        int pv = nums[start];
        for (int i = start + 1;i <= end; i ++) {
            int p = Integer.max(ppv + nums[i], pv);
            ppv = pv;
            pv = p;
        }
        return pv;
    }

}
